package com.lihui.response;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 功能描述
 *
 * @author wileda
 * @date 2023/04/03  16:07
 */
@WebServlet("/status")
public class StatusCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集编码
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //获取参数
        String type = req.getParameter("type");
        //根据不同的状态设置参数
        if(type.equals("1")){
            resp.setStatus(200);
        } else if (type.equals("2")) {
            resp.setStatus(404);
        } else if (type.equals("3")) {
            resp.setStatus(403);
        } else if (type.equals("4")) {
            resp.setStatus(500);
        }else {
            resp.setStatus(501);
        }
        resp.getWriter().write("返回的状态码是" + resp.getStatus());
    }
}
